package com.org.manhu.chargehelper.ui.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.org.manhu.chargehelper.R;
import com.org.manhu.chargehelper.adapter.AppListAdapter;
import com.org.manhu.chargehelper.adapter.NetAppListAdapter;
import com.org.manhu.chargehelper.broadcastreceiver.AcceptDataBroadcast;
import com.org.manhu.chargehelper.constant.Constant;
import com.org.manhu.chargehelper.custom.XListView;
import com.org.manhu.chargehelper.tools.JsonUtil;
import com.org.manhu.chargehelper.webservice.AsyncProcessData;

/**
 * @文件名：ExchangeListFragment.java
 * @包名：com.org.manhu.center
 * @作者：李朋辉 @QQ：892839211
 * @创建时间：2015年1月27日 上午11:55:25
 *
 * @描述：应用列表，用来加载服务器数据的fragment
 */
public class NetAppListFragment extends Fragment {

	private View view;
	private XListView mListView;
	private LinearLayout loadLayout;
	private NetAppListAdapter netAdapter;
	
	List<Map<String, String>> list = new ArrayList<Map<String, String>>();

	private AsyncProcessData request;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (Constant.APP_LIST.equals(msg.obj.toString())) {
				Log.d("TAG", "网络数据：：：：：："+msg.getData().getString("jsonStr"));
				List<Map<String, String>> data = JsonUtil.parseToList(msg
						.getData().getString("jsonStr"));
				if(data!= null){
					netAdapter.setData(data);
					netAdapter.notifyDataSetChanged();
				} 
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		view = inflater.inflate(R.layout.fragment_exchange_list, null);
		mListView = (XListView) view.findViewById(R.id.exchage_good_list);
		loadLayout = (LinearLayout) view
				.findViewById(R.id.exchage_good_load_layout);

		loadLayout.setVisibility(View.GONE);
		mListView.setVisibility(View.VISIBLE);
		mListView.setPullLoadEnable(false);
		mListView.setPullRefreshEnable(false);
		netAdapter = new NetAppListAdapter(getActivity(), list);
		mListView.setAdapter(netAdapter);

		request = new AsyncProcessData();

//		request.getRequestData(mHandler, Constant.APP_LIST);

		return view;
	}
	
	public void setData(List<Map<String, String>> list){
		this.list.addAll(list);
	}

	public void notify(List<Map<String, String>> list){
		netAdapter.setData(list);
		netAdapter.notifyDataSetChanged();
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

}
